package cn.lzj.order.service.impl;

import cn.lzj.basic.service.impl.BaseServiceImpl;
import cn.lzj.basic.util.LoginContext;
import cn.lzj.basic.util.PageList;
import cn.lzj.order.domain.OrderPetAcquisition;
import cn.lzj.order.dto.OrderDto;
import cn.lzj.order.mapper.OrderPetAcquisitionMapper;
import cn.lzj.order.query.OrderPetAcquisitionQuery;
import cn.lzj.order.service.IOrderPetAcquisitionService;
import cn.lzj.org.domain.Employee;
import cn.lzj.org.domain.Shop;
import cn.lzj.org.mapper.EmployeeMapper;
import cn.lzj.org.mapper.ShopMapper;
import cn.lzj.pet.domain.SearchMasterMsg;
import cn.lzj.pet.mapper.PetMapper;
import cn.lzj.pet.mapper.SearchMasterMsgMapper;
import cn.lzj.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public void remove(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //删除订单
        //将寻主消息中的shop_id置空，寻主池
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        petMapper.delete(order.getPetId());//删除对应的宠物id，没有成功收购
        super.remove(id);
    }

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoid(logininfo.getId());
        if (employee.getShop_id() != null) {//店员
            Shop shop = shopMapper.loadById(employee.getShop_id());
            if (shop.getAdmin_id() != employee.getId()) {
                query.setEmpId(employee.getId());
            }
            query.setShopId(shop.getId());
        }
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        Long orderId = orderDto.getId();
        Integer PayType = orderDto.getPayType();
        BigDecimal money = orderDto.getMoney();
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        //1.修改订单 state=1；
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(PayType);
        orderPetAcquisitionMapper.update(order);
        //2.支付方式修改
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        msg.setNote("寻主消息处理完成");
        searchMasterMsgMapper.update(msg);
        //3.修改状态
        //@TODO- 支付模块
    }
}